package com.reefe.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.reefe.dto.ItemDto;
import com.reefe.entity.Result;
import com.reefe.pojo.TbItem;
import com.reefe.service.ItemService;
import com.reefe.utils.ResultUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhao on 2017/12/21.
 */
@RestController
@RequestMapping("item")
public class ItemController {
    @Reference(version = "1.0.0")
    private ItemService itemServiceImpl;


    @RequestMapping(value="/{id}",method ={RequestMethod.GET} )
    public Result getItemById(@PathVariable("id") long itemId) {
        ItemDto itemDto=itemServiceImpl.getItemById(itemId);
        return new ResultUtil<ItemDto>().setData(itemDto);
    }


    /**
     * 查询商品列表
     * @param draw 请求的标记，代表次数
     * @param start 分页开始位置
     * @param length 每页显示条数
     * @param cid 类别ID
     * @param search 搜索关键字
     * @param orderCol 排序列下标
     * @param orderDir 排序方式 asc desc
     * @param searchItem 搜索内容
     * @param minDate 最小时间
     * @param maxDate 最大时间
     * @return DataTables 结果集
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public Map<String,Object> getItemList(int draw, int start, int length, int cid, @RequestParam("search[value]") String search,
                          @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir,
                          String searchItem, String minDate, String maxDate) {
        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "image", "title", "sell_point", "price", "created", "updated", "status"};
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }

        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        Map<String, Object> result = itemServiceImpl.getItemList(draw,start,length,cid,search,orderColumn,orderDir);
        return result;
    }



    /**
     * 搜索查询商品列表
     * @param draw 请求的标记，代表次数
     * @param start 分页开始位置
     * @param length 每页显示条数
     * @param cid 类别ID
     * @param search 搜索关键字
     * @param orderCol 排序列下标
     * @param orderDir 排序方式 asc desc
     * @param minDate 最小时间
     * @param maxDate 最大时间
     * @return DataTables 结果集
     */
    @RequestMapping(value = "/listSearch",method = RequestMethod.GET)
    public Map<String,Object> getItemSearchList(int draw, int start, int length,int cid,String searchKey,String minDate,String maxDate,
                                              @RequestParam("search[value]") String search, @RequestParam("order[0][column]") int orderCol,
                                              @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "image", "title", "sell_point", "price", "created", "updated", "status"};
        //默认排序列
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if(!search.isEmpty()){
            searchKey=search;
        }
        Map<String, Object> result = itemServiceImpl.getItemSearchList(draw,start,length,cid,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result<TbItem> addItem(ItemDto itemDto){

        TbItem tbItem=itemServiceImpl.addItem(itemDto);
        return new ResultUtil<TbItem>().setData(tbItem);
    }

    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public Map<String,Object> getAllItemCount(){
        Map<String,Object> result = new HashMap();
        long count = itemServiceImpl.getAllItemCount();
        result.put("recordsTotal",count);
        return result;
    }

    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public Result<TbItem> updateItem(@PathVariable("id") long id, ItemDto itemDto){
        itemDto.setId(id);
        TbItem tbItem=itemServiceImpl.updateItem(id,itemDto);
        return new ResultUtil<TbItem>().setData(tbItem);
    }

    @RequestMapping(value = "/del/{id}",method = RequestMethod.DELETE)
    public Result<TbItem> deleteItem(@PathVariable("id") long id){
        itemServiceImpl.deleteItem(id);
        return new ResultUtil<TbItem>().setData(null);
    }

    @RequestMapping(value = "/start/{id}",method = RequestMethod.PUT)
    public Result<TbItem> startItem(@PathVariable("id") long id){
        TbItem tbItem = itemServiceImpl.alertItemState(id,1);
        return new ResultUtil<TbItem>().setData(tbItem);
    }
    @RequestMapping(value = "/stop/{id}",method = RequestMethod.PUT)
    public Result<TbItem> stopItem(@PathVariable("id") long id){
        TbItem tbItem = itemServiceImpl.alertItemState(id,0);
        return new ResultUtil<TbItem>().setData(tbItem);
    }
}
